<?php

include(BASE . "subsystems/getid3/getid3.php");
$getid3 = new getID3();		
	
$album_type=0;
$page=0;
$music_limit=10;
$singer=$_REQUEST['search_string'];

// for sql injection
$singer=str_replace("'","",$singer);
$singer=str_replace('"',"",$singer);

if (isset($_GET['page'])) $page=intval($_GET['page']);

if (isset($_REQUEST['album_type']))
	$album_type=intval($_REQUEST['album_type']);

$start_at = $page  * $music_limit;
// get count all musics
$count_post =$db->countObjectsBySql("SELECT COUNT(website_musics.id) as c FROM website_listing  INNER JOIN website_musics  ON website_musics.album_id = website_listing.id WHERE (singer LIKE '%{$singer}%') AND album_type = {$album_type} ");

// cal page_count
// page split
		// cal how many posts
			$page_count=floor($count_post / $music_limit);
			if ($count_post % $music_limit != 0)
			  $page_count++;
			// build page array
			$pages = array();
			for ($i=1;$i<=$page_count;$i++)
				$pages[$i-1] = $i;

$musics=$db->selectObjectsBySql("SELECT website_musics.*  FROM website_listing  INNER JOIN website_musics  ON website_musics.album_id = website_listing.id WHERE (singer LIKE '%{$singer}%') AND (album_type = {$album_type})  LIMIT {$start_at},{$music_limit}");


for($j=0; $j<count($musics); $j++) {
		$album=$db->selectObject('listing','id ='.$musics[$j]->album_id);
		$musics[$j]->album_name = $album->name;
	$musics[$j]->downloadable=$album->downloadable;
	$musics[$j]->singer_search = htmlspecialchars($musics[$j]->singer);
				if ($musics[$j]->high_quality == 0) {
				$musics[$j]->high_quality = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
				$musics[$j]->high_quality = $file->directory.'/'.$file->filename;
				if (file_exists($musics[$j]->high_quality))
				{
				$file_info = $getid3->analyze($musics[$j]->high_quality); 				  
				  //getid3_lib::CopyTagsToComments($file_info); 
				  $musics[$j]->bitrate=round($file_info['bitrate']/1000,0);
				  $musics[$j]->fileformat=$file_info['fileformat'];
				  $file_info=$file_info['playtime_string'];
				  $musics[$j]->file_length = $file_info;
				 }
				}
				
				if ($musics[$j]->sound_trackmix == 0) {
				$musics[$j]->sound_trackmix = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sound_trackmix);
				$musics[$j]->sound_trackmix = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->sheet_music == 0) {
				$musics[$j]->sheet_music = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sheet_music);
				$musics[$j]->sheet_music = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->demo == 0) {
				$musics[$j]->demo = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->demo);
				$musics[$j]->demo = $file->directory.'/'.$file->filename;
				}
				
				// get composer name
				$composer=$db->selectObject('listing',"list_type = 1 AND id = {$musics[$j]->composer_id}");
				$composer_name='';
				if ($composer)
				{
					$composer_name=$composer->name;
				}
				
				// for tooltip
				$musics[$j]=musicsmodule::make_info($musics[$j]);
		}				
$template = new template('musicsmodule','_music_by_singer');		
$template->register_permissions(array('administrate','configure'),$loc);
$template->assign('musics',$musics);
$template->assign('page',$page);
$template->assign('page_count',$page_count);
$template->assign('pages',$pages);
$template->assign('album_type',$album_type);
$template->assign('album_show_require',1);
$template->assign('music_no_start',$start_at);
$template->assign('singer',$singer);

$template->output();
?>
